
auto.waitFor();

//-------------------------------------运行环境


//----------运行环境
unlock();
//-------------------------------------权限申请、手机设置
yinl0();
let 音量键监听=threads.start(registerKey);
toastLog('按音量下键来随时停止脚本');
toastLog('1秒后开始刷任务');
//------------------------------------环境区
let kd=device.width;let gd=device.height;let kdxs=kd/1080;let gdxs=gd/2340;
log('环境区over');
//-------------------------------------变量区
dyc=5000;  xyc=1000;  zyc=2000; let spyc=3200+Math.round(Math.random()*300);
let  cscs=0;  let jgarr=[];
let jg='';  let shuangkcs=0;  let ydcsz=0;let shuangk=1;let sspscxs=1.2;
let cc=1; let c=1;zbjr=0;
let apmc='1'; let appk='1'; let xm=1;let 执行进程=1; let djjg=0;
log('变量区over');

//-------------------------------------环境配置完成，开始执行
var jblj = "/sdcard/00js/";
files.removeDir(jblj);files.ensureDir(jblj);
console.hide()
//-----------------------------------------------主程序开始
shuangk=1;
while(shuangkcs<shuangk){
    try{
        for(i=1;i<10;i++){
            sleep(1000);log(i*3)
            log(engines.all());
        }

    } catch(err){toastLog('出错不影响双开应用继续刷，如未双开则已结束...')}
    console.show();
}

function fanqie_tix(){
    toastLog('第一次刷，每天一次提现');
    djzb('com.xs.fm:id/bb8',2);//领现金
    sleep(3000);toastLog('每天一次提现');
    xhmt(60);
    sleep(3000);
    djzb('立即提现');
}

function fanqie_kgg(){
    toastLog('开始领现金页面看广告领金币');
    djzb('com.xs.fm:id/bb8',2);//领现金
    sleep(3000);toastLog('开始领现金页面看广告领金币');
    djzb('立即签到');
    xhmt(60);
    sleep(3000);
    for (var itt=0;itt<9;itt++){
        itt++;
        toastLog('第'+itt+'次寻找领取金币')
        fanqie_linghb();
        shmt(1);sleep(1300);
    }
}

function fanqie_tings(){
    try{
        toastLog('去听书');
        djzb('com.xs.fm:id/sp',2);//首页
        sleep(3000);toastLog('找书');
        //点击推荐书中第一个
        djzb('com.xs.fm:id/bp',2);sleep(3000);toastLog('播放');
        djzb('开始播放');djzb('续播');sleep(3*1000);
    } catch(err){}
}

//领红包
function fanqie_linghb(){
    if(djzb('com.xs.fm:id/bbf',2)||djzb('立即领取')||djzb('开宝箱得',1)){
        sleep(3000);
        cc=text('立即领取').findOne(2000);
        djkj(cc);sleep(3000);
        cc=textContains('看视频领最多').findOne(2000);
        if(djkj(cc)){
            for(ii=1;ii<40;ii++){
                if(text('领取成功').findOne(1600)){toastLog('广告已看完');back();break;};
            }
        }
    }
    huisy('首页','番茄畅听');toastLog('回首页');
}


/**-------------------------------项目函数部分-------------------------------------------*/
function fanqie_hsy(){
    toastLog('尝试关闭弹窗');
    cc=className('com.lynx.tasm.ui.image.FlattenUIImage').depth(13).indexInParent(333).findOne(1000);
    djkj(cc);
    toastLog('回番茄首页');
    back();sleep(spyc);back();sleep(spyc);
    back();sleep(spyc);back();sleep(spyc);
    toastLog('已到首页')
}




//-------------------------------构建函数部分-------------------------------------------
//只写wz是text，ms=1是包含text，ms=2是id，ii是第几个，默认不写是0，ms=3是desc包含
function djzb(wz,ms,ii){
    if(ii>0){
        sleep(qjyc+3000);
        switch(ms)
        {
            case 1:
                var w = textContains(wz).boundsInside(0,0,kd+1,gd).findOnce(ii);
                break;
            case 2:
                var w = id(wz).boundsInside(0,0,kd+1,gd).findOnce(ii);
                break;
            case 3:
                var w = descContains(wz).boundsInside(0,0,kd+1,gd).findOnce(2000);
                break;
            default:
                var w = text(wz).boundsInside(0,0,kd+1,gd).findOnce(ii);
        }
    }
    else{ 
            switch(ms)
            {
                case 1:
                    var w = textContains(wz).boundsInside(0,0,kd+1,gd).findOne(3000);
                    break;
                case 2:
                    var w = id(wz).boundsInside(0,0,kd+1,gd).findOne(3000);
                    break;
                 case 3:
                    var w = descContains(wz).boundsInside(0,0,kd+1,gd).findOne(3000);
                    break;
                default:
                    var w = text(wz).boundsInside(0,0,kd+1,gd).findOne(3000);
            }
        }
    if(w!= null){
        var b = w.bounds();log(Math.max(b.centerX(),10), Math.max(b.centerY(),10));
        click(Math.max(b.centerX(),10), Math.max(b.centerY(),10));
        log('执行点击');log(b.left,b.top,b.right,b.bottom);
        return [b.left,b.top,b.right,b.bottom]
    }else{log("未点击");
            return 0}
}

function yinl0(){
    try {

        device.setMusicVolume(0)

        toastLog('成功设置媒体音量为0')

    		} catch (err) {

        alert('首先需要开启权限，请开启后再次运行脚本')

        exit()

    						}
};
//音量键停脚本
function registerKey() {
    events.observeKey()
    events.onKeyDown('volume_down', function (event) {
        console.log('脚本停止')
      //engines.myEngine().forceStop();
      //停止所有
      engines.stopAllAndToast();
      //停其他
//       engines.all().map((ScriptEngine) => {
//       if (engines.myEngine().toString() !== ScriptEngine.toString()) {
//       ScriptEngine.forceStop();
//       }
//       });
    })
};
function dkapp(dkapmc){
    var dkcs=1;dkappk = getPackageName(dkapmc);
    if(!dkappk){toastLog('未发现'+dkapmc+'安装包，此过程终止！');return 0};
    while(dkcs<9){
        sleep(qjyc+2000);log(launchApp(dkapmc));toastLog('等待打开');
        sleep(qjyc+3000);djzb('允许');djzb('跳过',1);
        c=packageName(dkappk).findOne(4000);
        if(c!= null){toastLog('已加载APP,未识别到双开'); return 1}
        else{
            toastLog('正尝试打开APP，如双开则自动选择APP');
            if (shuangkcs==1){click(dkapmc,0);} else {click(dkapmc,1);};
            shuangk=2;toastLog('已检测到此应用为双开应用');
            sleep(qjyc+2000);};
        djzb('跳过',1);sleep(qjyc+2000);djzb('允许');
        c=packageName(dkappk).findOne(7000);
        if(c!= null){toastLog('第'+dkcs+'次尝试打开APP成功');return 1}
        else {dkcs++;toastLog('第'+dkcs+'次尝试打开APP失败，最多重试8次，正尝试再次打开');  
        launchApp(yxhj);packageName(getPackageName(yxhj)).findOne(5000);};
        };
    if(c!= null){toastLog('已成功打开APP'+dkapmc);sleep(qjyc+5000);return 1}
        else{ toastLog('未能打开APP'+dkapmc);return 0}
}

//关闭APP
function gbapp(pknm) {
    try{
      home();sleep(qjyc+500);
      toastLog('先关闭APP'+pknm);
      var cmc = getPackageName(pknm);
      if(!cmc){toastLog('未发现安装包，跳过本过程');return 0};
      app.openAppSetting(cmc);
      toastLog('打开关闭设置');
      sleep(qjyc+6000);
      let is_sure = textMatches(/(.*强行.*|.*停止.*|.*结束.*|.*关闭.*|.*立即.*)/).find();
      if (is_sure)
      {
          for (var tz of is_sure){       log(1);
              try{click(tz.bounds().centerX(),tz.bounds().centerY());}catch(err){log(11)};
              sleep(qjyc+1000)};
          is_sure = textMatches(/(.*确定.*|.*强行.*|.*停止.*|.*结束.*|.*关闭.*|.*立即.*)/).find();
          for (var tz of is_sure){ 
            ww=tz.text().toString();if((ww.indexOf('悬浮窗')>0)){continue;};
              try{click(tz.bounds().centerX(),tz.bounds().centerY());}catch(err){log(11)};
              sleep(qjyc+1000)};
          sleep(qjyc+2000);
      } else {
          toastLog(app.getAppName(cmc) + "未在运行，不能关闭APP");
      }
      sleep(qjyc+100);back();sleep(qjyc+100);back();sleep(qjyc+100);home();sleep(qjyc+100);
    } 
    catch(err){log(err.massage())};
}
//回首页
function huisy(textt,apmca){
    if(apmca){} else{apmca=apmc};
    appka=getPackageName(apmca)
    for(icc=1;icc<8;icc++){
        var cc=text(textt).packageName(appka).findOne(1000);
        if(cc!= null){
             return 1;
        }else{
            back();sleep(qjyc+3000);}
        return 0
    }
}

// 解锁屏幕
function unlock()
{
  try{
    if(!device.isScreenOn())
    {		log(jiesuo);log(typeof(jiesuo));
        device.wakeUp();
        sleep(qjyc+500);
        swipe(500,1500,500,1000,210);
        sleep(qjyc+1500)
        var password = jiesuo  //这里输入你手机的密码
        for(var i = 0; i < password.length; i++)
        { log(password[i]);
            var p = text(password[i]).findOne(2000).bounds();
            click(p.centerX(), p.centerY());
            sleep(qjyc+100);
        }
    }}  catch(err){log('解锁错误，不支持小米手机')};
};


function shmt(cssa){
    for(ics=0;ics<cssa;ics++){
        swipe(80*kdxs+zbjr*10,gd*8/10,80*kdxs+zbjr*10,gd*4/10,200);
    };
}

function shww(cssa){
    for(ics=0;ics<cssa;ics++){
        swipe(80*kdxs+zbjr*10,450,80*kdxs+zbjr*10,280,200);
    };
}

function xhmt(cssa){
    for(ics=0;ics<cssa;ics++){
        swipe(80*kdxs+zbjr*10,280,80*kdxs+zbjr*10,450,200);
    };
}

//点击控件 控件、ms为0或无 点击中间，1右中，2左中,3 中上，4，中下
function djkj(kjmca,ms,pylx,pyly){
    var bbcx=10;var bbcy=10;
    if(pylx){} else {pylx=0};if(pyly){} else {pyly=0};
    if(kjmca){
        var bbc = kjmca.bounds();
        if(ms==1){bbcx=Math.max(bbc.right,10);bbcy=Math.max(bbc.centerY(),10)}
        else if(ms==2){bbcx=Math.max(bbc.right,10);bbcy=Math.max(bbc.centerY(),10)}
        else if(ms==3){bbcx=Math.max(bbc.centerX(),10);bbcy=Math.max(bbc.top,10)}
        else if(ms==4){bbcx=Math.max(bbc.centerX(),10);bbcy=Math.max(bbc.bottom,10)}
        else {bbcx=Math.max(bbc.centerX(),10);bbcy=Math.max(bbc.centerY(),10)}
        click(bbcx+pylx,bbcy+pyly);
        log('找到控件，执行点击koj'); return 1
    } else {log('未找到控件，未点击'); return 0}
}


